<template>
	<div class="box">
		<ul>
			<li v-for="(item,index) in phoneArr" v-if="type == 'userPhone'" :key="item._id">
				<span>会员账号：{{item.phone }}</span><span @click="del(index , 'userPhone')" class='del'>删除</span></li>

			<li v-for="(item,index) in phoneArr" v-if="type == 'nai'" :key="item._id">
				<span>品种名称：{{item.name}}</span><span @click="del(index , 'nai')" class='del'>删除</span></li>
				
			<li v-for="(item,index) in phoneArr" v-if="type == 'area'" :key="item._id">
				<span>地区名称：{{item.name}}</span><span @click="del(index , 'area')" class='del'>删除</span></li>
		</ul>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				phoneArr: [],
				type: "user"
			}
		},
		onLoad(option) {
			console.log(option);
			this.type = option.type
			wx.cloud.init({
				traceUser: true,
			})
			// let currentRoute = currentRoutes[currentRoutes.length - 1].route //获取当前页面路由
			this.init()
		},
		methods: {
			init() {
				if (this.type == 'nai') {
					this.apiList("nai")
				} else if (this.type == 'userPhone') {
					this.apiList("userPhone")
				}else if(this.type == 'area'){
					this.apiList("area")
				}
			},

			apiList(mysql) {
				let parmse = {}
				if(mysql == 'area'){
					parmse.phone = uni.getStorageSync("phone");
				}
				console.log("来了");
				uniCloud.callFunction({
					name: mysql, //参数是云函数的名 会自动找这个云函数
					data:parmse
				}).then(res => {
					console.log(res);
					this.phoneArr = res.result.data
					console.log(this.phoneArr);
				})
			},

			// 删除
			del(index, mysql) {
				let _flag = this
				let obj = this.phoneArr[index]
				uni.showModal({
					title: '提示',
					content: '确认删除该条信息吗？',
					success: function(res) {
						if (res.confirm) {
							uniCloud.callFunction({
								name: "userPhone_nai_del",
								data: {
									_id: obj._id,
									name: mysql
								}
							}).then(res => {
								if (res.result.code == 200) {
									uni.showToast({
										title: '删除成功',
										//将值设置为 success 或者直接不用写icon这个参数
										icon: 'success',
										//显示持续时间为 2秒
										duration: 2000
									})
									_flag.init()
								} else {
									uni.showToast({
										title: '删除失败',
										//将值设置为 success 或者直接不用写icon这个参数
										icon: 'error',
										//显示持续时间为 2秒
										duration: 2000
									})
								}

							})

						} else {
							// 执行取消后的操作
							uni.showToast({
								title: '取消删除成功',
								//将值设置为 success 或者直接不用写icon这个参数
								icon: 'success',
								//显示持续时间为 2秒
								duration: 2000
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
	.box {
		width: 100%;
		height: 100%;
		overflow: scroll;
		border-radius: 5px;

	}

	.box ul li {
		width: 100%;
		height: 50px;
		display: flex;
		padding: 10px;
		box-sizing: border-box;
		align-items: center;
		justify-content: space-between;
		background-color: #fff;
		border-radius: 10px;
		margin: 10px 0;
		box-shadow: 0 0 5px 0 rgba(0, 0, 0, .3);

	}

	.del {
		color: red;
	}
</style>